Sửa đổi Git
Cam kết Git --amend
commit --amend
được sử dụng để sửa đổi commit
gần đây nhất.
Nó kết hợp những thay đổi trong staging environment
với commit
mới nhất và tạo ra một commit
mới.
commit
mới này thay thế hoàn toàn commit
mới nhất.
Thông báo cam kết sửa đổi Git
Một trong những điều đơn giản nhất bạn có thể làm với --amend
là thay đổi thông điệp commit
.
Hãy cập nhật README.md
và commit
:
Ví dụ
git commit -m "Adding plines to reddme" [master 07c5bc5] Adding plines to reddme 1 file changed, 3 insertions(+), 1 deletion(-)
Bây giờ hãy kiểm tra log
:
Ví dụ
git log --oneline 07c5bc5 (HEAD -> master) Adding plines to reddme 9a9add8 (origin/master) Added .gitignore 81912ba Corrected spelling error 3fdaa5b Merge pull request #1 from w3schools-test/update-readme 836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta facaeae (gh-page/master) Merge branch 'master' of https://github.com/w3schools-test/hello-world e7de78f Updated index.html. Resized image 5a04b6f Updated README.md with a line about focus d29d69f Updated README.md with a line about GitHub e0b6038 merged with hello-world-images after fixing conflicts 1f1584e added new image dfa79db updated index.html with emergency fix 0312c55 Added image to Hello World 09f4acd Updated index.html with a new line 221ec6e First release of Hello World!
Ôi không! thông báo commit
đầy lỗi chính tả. Xấu hổ. Hãy amend
điều đó:
Ví dụ
git commit --amend -m "Added lines to README.md" [master eaa69ce] Added lines to README.md Date: Thu Apr 22 12:18:52 2021 +0200 1 file changed, 3 insertions(+), 1 deletion(-))
Và kiểm tra lại log
:
Ví dụ
git log --oneline eaa69ce (HEAD -> master) Added lines to README.md 9a9add8 (origin/master) Added .gitignore 81912ba Corrected spelling error 3fdaa5b Merge pull request #1 from w3schools-test/update-readme 836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta facaeae (gh-page/master) Merge branch 'master' of https://github.com/w3schools-test/hello-world e7de78f Updated index.html. Resized image 5a04b6f Updated README.md with a line about focus d29d69f Updated README.md with a line about GitHub e0b6038 merged with hello-world-images after fixing conflicts 1f1584e added new image dfa79db updated index.html with emergency fix 0312c55 Added image to Hello World 09f4acd Updated index.html with a new line 221ec6e First release of Hello World!
Chúng tôi thấy commit
trước đó được thay thế bằng cam kết đã sửa đổi của chúng tôi!
Cảnh báo: Việc xáo trộn lịch sử commit
của kho lưu trữ có thể nguy hiểm. Thông thường, bạn có thể thực hiện những loại thay đổi này đối với kho lưu trữ cục bộ của riêng mình. Tuy nhiên, bạn nên tránh thực hiện các thay đổi ghi lại lịch sử vào kho lưu trữ remote
, đặc biệt nếu những người khác đang làm việc với chúng.
Tập tin sửa đổi Git
Việc thêm tệp bằng --amend
cũng hoạt động theo cách tương tự như trên. Chỉ cần thêm chúng vào staging environment
trước khi cam kết.